﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="lyt.aspx.cs" Inherits="lyt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <link href="Styles/lyt.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">        

        function request(paras) {
            var url = location.href;
            var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
            var paraObj = {}
            for (i = 0; j = paraString[i]; i++) {
                paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
            }
            var returnValue = paraObj[paras.toLowerCase()];
            if (typeof (returnValue) == "undefined") {
                return "";
            } else {
                return returnValue;
            }
        }

        $(document).ready(function () {

            var tokenVal = $("#token").val();
            var codeVal = request('code');

            $.post("https://oauth.tbsandbox.com/token", { response_type: "code", client_id: "1021236413", redirect_uri: "http://localhost:53271/lyt/lyt.aspx", scope: "item", view: "web", code: codeVal,
                client_secret: "sandbox17043c6b462896102f96efd39", grant_type: "authorization_code"
            }, function (data) {
                alert("ok");
            }, "json")

            $("#submit").click(function () {
                var codeVal = request('code');
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "http://localhost:53271/lyt/test.ashx",
                    data: { code: codeVal },
                    success: function (data) {
                        $.each(data.items_onsale_get_response.items.item, function (i, n) {
                            var row = $("#template").clone();
                            row.find("#num_iid").text(n.num_iid);
                            row.find("#title").text(n.title);
                            row.find("#price").text("￥" + n.price);
                            if (n.cost != undefined) { row.find("#cost").text("￥" + n.cost); }
                            else { row.find("#cost").text("无"); }
                            row.find("#more").html("<a href=OrderInfo.aspx?id=" + n.num_iid + ">&nbsp;More</a>");
                            row.attr("id", "ready"); //改变绑定好数据的行的id
                            row.appendTo("#datas"); //添加到模板的容器中                            
                        });

                        $("tbody>tr:odd").addClass("odd");
                        $("tbody>tr:even").addClass("even");

                        $('tbody>tr').click(function () {
                            $(this)
                                .addClass('selected')
                                .siblings().removeClass('selected')
                                .end()
                        });
                    }
                });
            });

            $("#submit1").click(function () {
                $.post("https://oauth.tbsandbox.com/token", { response_type: "code", client_id: "1021236413", redirect_uri: "http://localhost:53271/lyt/lyt.aspx", scope: "item", view: "web", code: codeVal,
                    client_secret: "sandbox17043c6b462896102f96efd39", grant_type: "authorization_code"
                }, function (data) {
                    alert(data.toString());
                }, "json")
            });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" id="submit" value="获得商品数据" />
        <input type="button" id="submit1" value="获得Token" />        
        <div id="myDiv" style="width:100%">
            <table id="datas" border="1" cellspacing="0" style="border-collapse: collapse">
                <thead>
                    <tr>
                    <th>
                        订单ID</th>
                    <th>
                        标题</th>
                    <th>
                        售价</th>
                    <th>
                        成本</th>
                    <th>
                        操作</th>                    
                </tr>
                </thead>
                <tbody>
                    <tr id="template">
                    <td id="num_iid">
                    </td> 
                    <td id="title">
                    </td>
                    <td id="price">
                    </td> 
                    <td id="cost">
                    </td> 
                    <td id="more">
                    </td>                 
                </tr>
                </tbody>                
            </table>
        </div>
    </div>
    </form>
</body>
</html>
